//
//  LearnCenterViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "LearnCenterViewController.h"
#import "LearnCenterHeader.h"
#import "LearnCenterTableViewCell.h"
#import "LearnInfoViewController.h"
#import "LearningModel.h"

@interface LearnCenterViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, copy)NSString * type;
@property (nonatomic, strong)NSArray<LearningModel *> * modelArray;

@end

@implementation LearnCenterViewController


- (void)setModelArray:(NSArray<LearningModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.type = @"5";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLearningData:) name:@"changeLearningData" object:nil];
    
    [self loadDataWithType];
    [self setupUI];
}


- (void)loadDataWithType{
    
    [LearningModel learningModelWithType:self.type WithSucess:^(NSArray<LearningModel *> *array) {
        
        self.modelArray = array;
    } error:^{
        
    }];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 94 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[LearnCenterTableViewCell class] forCellReuseIdentifier:@"LearnCenterListCell"];
    [self.tableView registerClass:[LearnCenterHeader class] forHeaderFooterViewReuseIdentifier:@"LearnHeaderView"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LearnCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LearnCenterListCell" forIndexPath:indexPath];
    
//    cell.titleLabel.text = self.showStringArray[indexPath.row];
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    LearnCenterHeader * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LearnHeaderView"];
    
    return header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    NSString * titleStr;
    
    if ([self.type isEqualToString:@"5"]) {
        
        titleStr = @"面相分析";
    }else if ([self.type isEqualToString:@"6"]){
        
        titleStr = @"手相分析";
    }else if ([self.type isEqualToString:@"7"]){
        
        titleStr = @"痣相分析";
    }else if ([self.type isEqualToString:@"8"]){
        
        titleStr = @"家具分析";
    }
    
    LearningModel * model = self.modelArray[indexPath.row];
    
    LearnInfoViewController * infoVC = [LearnInfoViewController new];
    
    infoVC.titleStr = titleStr;
    infoVC.model = model;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (void)changeLearningData:(NSNotification *)noti{
    
    self.type = noti.userInfo[@"type"];
    
    [self loadDataWithType];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
